package com.kobeliu.controller;

import com.kobeliu.api.interceptor.JwtCurrentUserInterceptor;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.enums.JobStatus;
import com.kobeliu.pojo.Admin;
import com.kobeliu.pojo.Job;
import com.kobeliu.pojo.Users;
import com.kobeliu.pojo.bo.EditJobBO;
import com.kobeliu.pojo.bo.SearchJobsBO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.JobService;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/job")
@Slf4j
public class JobController extends BaseInfoProperties {

    @Autowired
    private JobService jobService;

    @PostMapping("hr/jobList")
    public GraceJSONResult hrJobList(
            String hrId
            ,String companyId
            ,Integer page
            ,Integer limit
            ,Integer status
    ){

        if(page ==null) page = 1;
        if(limit == null) limit = 10;

        if(status==0){
            return GraceJSONResult.ok(jobService.hrJobAllList(hrId,companyId,page,limit));
        }else{
            return GraceJSONResult.ok(jobService.hrJobList(hrId,companyId,page,limit,status));
        }

    }

    /**
     * 职位数
     * @param hrId
     * @return
     */
    @PostMapping("hr/jobCounts")
    public GraceJSONResult hrJobCounts(String hrId){

        long count = jobService.getJobCounts(hrId);
        return GraceJSONResult.ok(count);
    }

    @PostMapping("hr/jobDetail")
    public GraceJSONResult jobDetail(String hrId,String companyId,String jobId){

        Job job = jobService.jobDetail(hrId,companyId,jobId);

        return GraceJSONResult.ok(job);
    }

    @PostMapping("modify")
    public GraceJSONResult jobDetail(@RequestBody EditJobBO bo){

        jobService.modifyJob(bo);

        return GraceJSONResult.ok();
    }

    @PostMapping("close")
    public GraceJSONResult closeJob(String hrId,String companyId,String jobId){

        jobService.closeJob(hrId,companyId,jobId);

        return GraceJSONResult.ok();
    }

    @PostMapping("open")
    public GraceJSONResult openJob(String hrId,String companyId,String jobId){

        jobService.openJob(hrId,companyId,jobId);

        return GraceJSONResult.ok();
    }

    /**
     * 候选人搜索职位
     * @param bo
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("searchJobs")
    public GraceJSONResult searchJobs(@RequestBody SearchJobsBO bo, Integer page, Integer limit){

        if(page ==null) page = 1;
        if(limit == null) limit = 10;

        PagedGridResult pagedGridResult = jobService.searchJobs(bo,page,limit, JobStatus.OPEN);

        return GraceJSONResult.ok(pagedGridResult);
    }

    //================admin使用=================

    @PostMapping("jobList")
    public GraceJSONResult hrJobList(Integer page,Integer limit){
        if(page == null) page = 1;
        if(limit == null) limit = 10;

        //获取UserId信息
        Users users = JwtCurrentUserInterceptor.currentUser.get();

        PagedGridResult result = jobService.adminJobList(users.getHrInWhichCompanyId(),page,limit);
        return GraceJSONResult.ok(result);
    }

    @PostMapping("admin/jobDetail")
    public GraceJSONResult hrJobList(String jobId){

        return GraceJSONResult.ok(jobService.adminJobDetail(jobId));
    }
    //================admin使用=================


}
